package com.zaofan.dao;

import com.zaofan.bean.OrderInfo;
import com.zaofan.constant.StateConstant;
import com.zaofan.dao.base.DaoSupportImpl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderInfoDao extends DaoSupportImpl<OrderInfo> {

	private static Logger logger = LoggerFactory.getLogger(OrderInfoDao.class);

	public List<OrderInfo> findByAcccountId(Integer accountId, Integer begin, Integer end) {
		Map<String, Object> params = new HashMap<>();
		params.put("accountId", accountId + "");
		params.put("begin", begin + "");
		params.put("end", end + "");
		params.put("parentId", "0");
		params.put("isTimeOut", "0");
		params.put("order", " by orderTime desc,id desc");
		return findAll(params);
	}

	public OrderInfo findById(Integer orderId) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", orderId);
		return findOne(params);
	}
    public OrderInfo findByOrderNo(String orderNo) {
        Map<String, Object> params = new HashMap<>();
        params.put("orderNo", orderNo);
        return findOne(params);
    }
	public OrderInfo findParentByOrderNo(String orderNo){
		Map<String, Object> params = new HashMap<>();
		params.put("orderNo", orderNo);
		params.put("parentId",0);
		return findOne(params);
	}

	public void delOrder(OrderInfo info) {

	}

	public List<OrderInfo> findByParentId(Integer orderId) {
		Map<String, Object> params = new HashMap<>();
		params.put("parentId", orderId);
		return findAll(params);
	}

	public  List<OrderInfo> findByUserMobile(String mobile){
		Map<String, Object> params = new HashMap<>();
		params.put("userMobile", mobile);
		return findAll(params);
       }


	public void updateaOrderPayState(String orderNo) {
		try {
			String sql = "update " + OrderInfo.class.getSimpleName().toLowerCase() + " set state = "
					+ StateConstant.ORDER_STATE_PAY + " where orderNo = '" + orderNo + "'";
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
    public void updateRefund(String orderNo){

		String sql ="update orderinfo set payState = 4 where orderNo = '" + orderNo + "'";
		logger.info(sql);
		QueryRunner runner = new QueryRunner(getDataSource());
		try {
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	public boolean isFirstOrder(int id) {
		try {
			String sql = "select count(*) from orderinfo where accountId = "
					+ id + " and state in (0,1,2)  and isTimeOut = 0";
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			Long count = runner.query(sql, new ScalarHandler<Long>());
			if (count == 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean isFirstUserMobile(String userMobile) {
		try {
			String sql = "select count(*) from " + OrderInfo.class.getSimpleName().toLowerCase() + " where userMobile = '"
					+ userMobile + "' and isTimeOut = 0";
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			Long count = runner.query(sql, new ScalarHandler<Long>());
			if (count == 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean isFirstDevice(String deviceId) {
		try {
			String sql = "select count(*) from " + OrderInfo.class.getSimpleName().toLowerCase() + " where deviceId = '"
					+ deviceId + "' and isTimeOut = 0";
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			Long count = runner.query(sql, new ScalarHandler<Long>());
			if (count == 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean isFirstPayedOrder(int id) {
		try {
			String sql = "select count(*) from " + OrderInfo.class.getSimpleName().toLowerCase() + " where accountId = "
					+ id + " and isTimeOut = 0 and payState = 2";
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			Long count = runner.query(sql, new ScalarHandler<Long>());
			if (count == 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean isAreaExtendFirstOrder(String userMobile,String deviceId)
	{
		try {
			String sql1 = "select count(*) from promotion_record where deviceId = "+deviceId;
			String sql2 = "select count(*) from promotion_record where accountMobile = "+userMobile;
			logger.info(sql1);
			QueryRunner runner = new QueryRunner(getDataSource());
			Long count1 = runner.query(sql1, new ScalarHandler<Long>());
			if (count1 == 0) {
				QueryRunner runner1 = new QueryRunner(getDataSource());
				Long count2 = runner1.query(sql2, new ScalarHandler<Long>());
				if(count2==0)
				{
					return true;
				}
			}
			else
			{
				return false;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	/*是否已享受过马拉松活动优惠*/
	public boolean isActivityOrder(String accountMobile)
	{
		String sql = "SELECT COUNT(*) FROM activity_record WHERE accountMobile = "+accountMobile;
		QueryRunner runner = new QueryRunner(getDataSource());

		try {
			Long count = runner.query(sql, new ScalarHandler<Long>());
			if(count>0)
			{
				return true;
			}
			else
			{
				return false;
			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return false;



	}

}
